local gamelib = require "club.threecard.gamelib"
local game = require "club.threecard.bjracing"

local ingame = gamelib.ui.activity();

local settlement = game.require ("gameui.settlement.settlement").new()
settlement:hide()

ingame.store_watchers = 
{
    ingame = 
    {
        ["发牌"] = function(self, store, state, args, isInitialize)
            if isInitialize then
                return;
            end
            self:on_deal(args.card_uids);
        end,
        ["看牌"] = function(self, store, state, args, isInitialize)
            if isInitialize then
                return;
            end
            self:on_see(args.card_uids);
        end,
        ["换牌"] = function(self, store, state, args, isInitialize)
            if isInitialize then
                return;
            end
            self:on_exchange(args.card_uids);
        end,
        ["on_ope_status"] = function(self, store, state, args)
            self:on_ope_status(args);
        end,
        ["结算"] = function(self, store, state, args, isInitialize)
            if isInitialize then
                return;
            end

            settlement:show();
        end
    }
}

function ingame:ctor()
    self.res = "club.threecard.bjracing/ui/gamescene.b:gamescene";
    self.app = game;
    local containerConfig = game.require "game.PokerContainerConfig_gamescene"
    self.poker_container = game.pokerlib.core.poker_container.new(containerConfig);
    self.phase_shower = game.require("gameui.gamescene.phase_shower").new();
    self.phase_buttons = game.require("gameui.gamescene.phase_buttons").new(self.poker_container);
    self.player_info = game.require("gameui.gamescene.player_info").new();
    self.game_info = game.require("gameui.gamescene.game_info").new();
    self.rank_list = game.require("gameui.gamescene.rank_list").new();
end

function ingame:load()
    self:super("load");
    self.phase_shower:load();
    self.phase_shower:set_parent(self.root.transform, false);
    self.phase_buttons:load();
    self.phase_buttons:set_parent(self.root.transform, false);
    self.player_info:load();
    self.player_info:set_parent(self.root.transform, false);
    self.game_info:load();
    self.game_info:set_parent(self.root.transform, false);
    self.rank_list:load();
    self.rank_list:set_parent(self.root.transform, false);

    settlement:load();
end

function ingame:on_injected(bind)
    self.poker_container:set_gameobject(self.poker_container_obj, true);
    bind("button.click", self.btn_exit, self.exit_ivk);
end

function ingame:exit_ivk()
    self.poker_container:clear();
    game.game_scene.leave();
    game.router.go("/"..game.package.."/main");
end

function ingame:on_deal(card_uids)
    self.poker_container:clear();
    self.poker_container:mopai(card_uids);
end

function ingame:on_see(card_uids)
    local pokers = self.poker_container.hand;
    if #pokers == 0 then
        self.poker_container:mopai(card_uids);
        return;
    end

    for index, poker in ipairs(pokers) do
        local value = card_uids[index];
        if poker.data.server_value ~= value then
            local pokerData = self.poker_container.config.poker_data_class.new(value);
            poker:Flip(pokerData);
        end
    end
end

function ingame:on_exchange(card_uids)
    local pokers = self.poker_container.hand;
    if #pokers == 0 then
        self.poker_container:mopai(card_uids);
        return;
    end

    for index, poker in ipairs(pokers) do
        local value = card_uids[index];
        if poker.data.server_value ~= value then
            local pokerData = self.poker_container.config.poker_data_class.new(value);
            poker:Flip(pokerData);
        end
    end
end

function ingame:on_ope_status(args)
    local can_see = args.can_see;
    local can_bet = args.can_bet;
    local can_exchange = args.can_exchange;

    if can_see or can_exchange then
        self.poker_container.config.ope_valid = true;
    else
        self.poker_container.config.ope_valid = false;
        local pokers = self.poker_container.hand;
        for _, poker in pairs(pokers) do
            poker:Unselect()
        end
    end
end

function ingame:set_layer(layer)
    settlement:set_layer(layer + 1);
    self:super("set_layer", layer);
end

return ingame